//渲染表格
renderTable(getItem("tableList"));

//初始化表格，生成标题和列名
function initTable() {
    let tableDom = document.querySelector("#list");
    tableDom.innerHTML = "";

    //标题
    let thead = document.createElement("thead");
    thead.innerHTML = `<tr><td class="tdName">材料名</td><td class="tdTime">计时</td><td class="tdBtn">操作</td></tr>`;
    tableDom.appendChild(thead);

    let tbody = document.createElement("tbody");
    tbody.id = "tbody";
    tableDom.appendChild(tbody);

}

function renderTable(list) {
    initTable();
    if (list != null && list != {}) {
        for (let name in list) {
            let lastCompleteTime = list[name];
            addTable(name, getState(name, lastCompleteTime))
        }
    }
}

//判断当前材料剩余刷新状态
function getState(name, lastComplteTime) {

    let nowTimestamp = getTimestamp();
    let refreshTime = timeList[name]["refreshTime"];
    if(timeList[name].type == "time"){  //多长时间后刷新
        if ((nowTimestamp - lastComplteTime) >= refreshTime) {
            return "已刷新 " + sec2time(nowTimestamp - lastComplteTime - refreshTime);
        }
        else {
            return sec2time(refreshTime - nowTimestamp + lastComplteTime) + " 后刷新";
        }
    }
    else if(timeList[name].type == "days"){ //几天后的凌晨0点刷新
        let today = lastComplteTime - (lastComplteTime + 8*3600) % (1*24*3600);
        let nextCompleteTime = today + timeList[name].refreshTime;
        if(nextCompleteTime < nowTimestamp){
            return "已刷新 " + sec2time(nowTimestamp - nextCompleteTime);
        }
        else{
            return sec2time(nextCompleteTime - nowTimestamp) + " 后刷新";
        }
    }
    else{
        return;
    }
    

}

function addTable(name, remainingTime) {
    let tr = document.createElement("tr");
    tr.classList.add(name);
    tr.classList.add("tableList");

    let tdName = document.createElement("td");
    tdName.classList.add("tdName");
    tdName.innerHTML = name;
    tr.appendChild(tdName);

    let tdTime = document.createElement("td");
    tdTime.classList.add("tdTime");
    tdTime.innerHTML = remainingTime;
    tr.appendChild(tdTime);

    let tdBtn = document.createElement("td");
    tdBtn.classList.add("tdBtn");
    tdBtn.innerHTML = `<div class="layui-input-inline"><button class="layui-btn layui-btn-normal layui-btn-sm" id="btn_reset" onclick="reset(this)">重置</button>`;
    tdBtn.innerHTML += `<button class="layui-btn layui-btn-danger layui-btn-sm" id="btn_delete" onclick="del(this)">删除</button></div>`;
    tr.appendChild(tdBtn);

    document.querySelector("tbody").appendChild(tr);
}

function changeTable(name, remainingTime) {
    let tr = document.querySelector("." + name);
    if (tr != null) {
        tr.cells[1].innerHTML = remainingTime;
    }
    else {
        addTable(name, remainingTime);
    }
}

function reset(obj) {
    let row = obj.parentNode.parentNode.parentNode;
    let name = row.cells[0].innerHTML;

    layui.use("layer", function () {
        var layer = layui.layer;
        layer.confirm("重置将会使该材料刷新时间重新计时。", { title: "是否重置" }, function (index) {
            //do something
            //数据重置
            let tableList = getItem("tableList");
            tableList[name] = getTimestamp();
            setItem("tableList", tableList);

            //表格数据重置
            row.cells[1].innerHTML = getState(name, getTimestamp());
            layer.close(index);
        });
    });
}

function del(obj) {
    let row = obj.parentNode.parentNode;
    let name = row.cells[0].innerHTML;
    layer.confirm("删除后该材料将不再计时。", { title: "是否删除" }, function (index) {
        //do something
        //数据删除
        let tableList = getItem("tableList");
        delete tableList[name];
        setItem("tableList", tableList);

        //表格删除
        row.parentNode.removeChild(row);
        layer.close(index);
    });

}